import axios from 'axios'
import qs from 'qs'

var instance = axios.create({
    baseURL: '/api',
    timeout: 20000
})

const get = async function (url, params) {
    const { data } = await instance.get(url, { params })
    return data
}

const post = async function (url, params, form = false) {
    if (form) {
        const query = qs.stringify(params)
        params = query
    }
    const { data } = await instance.post(url, params)
    return data
}

// 请求拦截器
instance.interceptors.request.use(function (config) {
    config.headers.token = window.sessionStorage.getItem('token');
    return config;
});

// 响应拦截器
const reponseInterceptorsFunSuc = function (response) {
    return response
}

const reponseInterceptorsFunError = function (error) {
    return Promise.reject(error)
}

instance.interceptors.response.use(reponseInterceptorsFunSuc, reponseInterceptorsFunError)

export {
    get,
    post
}
